<?php
namespace App\Services;

use Illuminate\Http\Request;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
// use GuzzleHttp\Pool;
// use GuzzleHttp\Exception\ClientException;

/**
* 期刊 2019.8.2
*/
class qikan
{
    //协议
    private $protocol = 'http://';
    //base url
    private $base_url = 'dps.qikan.com/';
    //api
    private $api = 'api/';
    //magazine
    private $magazine = 'magazine/';
    private $article = 'article/';
    private $GetFullDetail = 'GetFullDetail/';
    private $search = 'search';
    private $issues = 'Issues';
    private $catalog = 'catalog';
    private $info = 'Info';
    private $articleid = 'articleid';
    private $account = 'buu.';
    private $apptoken = 'ebtsKZwpbrhBPacH9FYH%2FQk79YBYHvtRqUYG1NsLbk%2Bs68eU7vDkbI1Lc39MDgnB%2Frys0F0BGSc%3D';

    private $ticket = 'FbU1HeHx3sahb3jiHRiN98VS65yGzQ5qW3Tk3iFYx2PbV5hE1QZLlt0iG1Yi9XkejPI4AiNrtjH72f5pgSnRX0L4CnPVJPYyjAcNHdr2OGx0Kb7iWvn56H0Wm9GPZIqKDUVFjvJMm6--1rarUmcJjvOxoMe6DWJz7YFFiTMy_ukWxLmm_vyADGIVKYFQs53S';

    private $pagesize = '100';
    private $pageindex = '1';
    private $itemcount = '0';
    private $client;

    protected $api_data;
    protected $insert_data;
    protected $out_put_data;

    public function __construct()
    {
        $this->client = new Client();
    }
    /**
     * 模糊搜索书籍名称
     * @param  [string] $name [书籍名称]
     * @return [type]       [description]
     */
    public function searchByname($name)
    {
        //拼接url
        $url = $this->protocol .
               $this->account .
               $this->base_url .
               $this->api .
               $this->magazine .
               $this->search .
               '?apptoken=' . $this->apptoken .
               '&ticket=' .  $this->ticket .
               '&keyword=' . $name .
               '&pagesize=' . $this->pagesize .
               '&pageindex=' . $this->pageindex .
               '&itemcount=' . $this->itemcount;

        $guzz_respone = $this->client->get($url);

        //判断通信状态码
        if ($guzz_respone->getStatusCode() == 200) {
            //获取响应内容
            $contents = $guzz_respone->getBody()->getContents();

            $this->api_data = json_decode($contents);
            return $this;
            //判断响应内容是否合法
            // if ($this->api_data->Code == 1) {
                // return $this->toQikanFields($content->Data);
            // }
        }
    }
    /**
     * [getQikanListById 获取期刊列表 ]
     * @param  int $id [期刊id]
     * @return [obj] $guzz_respone
     */
    public function getQikanListById($id='')
    {
        //拼接url
        $url = $this->protocol .
               $this->account .
               $this->base_url .
               $this->api .
               $this->magazine .
               $this->issues .
               '?apptoken=' . $this->apptoken .
               '&ticket=' .  $this->ticket .
               '&id=' . $id;

        $guzz_respone = $this->client->get($url);
        //判断通信状态码
        if ($guzz_respone->getStatusCode() == 200) {
            //获取响应内容
            $contents = $guzz_respone->getBody()->getContents();

            $this->api_data = json_decode($contents);
            return $this;

            // //判断响应内容是否合法
            // if ($content->Code == 1) {
            //     return $this->toQikanFields($content->Data);
            // }
        }
    }
    //获取某期期刊目录
    public function getCatalogListById($id, $year, $issue)
    {
        //拼接url
        $url = $this->protocol .
               $this->account .
               $this->base_url .
               $this->api .
               $this->magazine .
               $this->article .
               $this->catalog .
               '?apptoken=' . $this->apptoken .
               '&ticket=' .  $this->ticket .
               '&magazineguid=' . $id .
               '&year=' .  $year .
               '&issue=' .  $issue;
        //发送请求
        $guzz_respone = $this->client->get($url);
        //判断通信状态码
        if ($guzz_respone->getStatusCode() == 200) {
            //获取响应内容
            $contents = $guzz_respone->getBody()->getContents();

            $this->api_data = json_decode($contents);
            return $this;
        }
    }
    //获取期刊内文章详情
    public function getContentById($id)
    {
        //拼接url
        $url = $this->protocol .
               $this->account .
               $this->base_url .
               $this->api .
               $this->magazine .
               $this->article .
               $this->GetFullDetail .
               '?apptoken=' . $this->apptoken .
               '&ticket=' .  $this->ticket .
               '&articleid=' . $id;
        //发送请求
        $guzz_respone = $this->client->get($url);
        //判断通信状态码
        if ($guzz_respone->getStatusCode() == 200) {
            //获取响应内容
            $contents = $guzz_respone->getBody()->getContents();

            $this->api_data = json_decode($contents);
            return $this;
        }
    }
    public function setApiData($data)
    {
       $this->api_data = $data;
    }
    public function getApiData()
    {
        return $this->api_data;
    }
    public function setInsertData($data)
    {
       $this->insert_data = $data;
    }
    public function getInsertData()
    {
        return $this->insert_data;
    }
    public function setOutPutData($data)
    {
       $this->out_put_data = $data;
    }
    public function getOutPutData()
    {
        return $this->out_put_data;
    }
    //格式转换对应数据库字段--期刊的格式--输入格式
    public function toQikanFields()
    {
        // dump($this->api_data);
        if ($this->api_data->Code != 1) {
            return false;
        }

        $data = $this->api_data->Data;

        $new_data = [];

        foreach ($data as $key => $value) {
            $new_data[$key]['book_id'] = $value->ID;

            if (isset($value->Name)) {
                $new_data[$key]['book_name'] = $value->Name;
            }
            if (isset($value->Year)) {
                $new_data[$key]['current_update_year'] = $value->Year;
            }
            if (isset($value->Issue)) {
                $new_data[$key]['current_update_month'] = $value->Issue;
            }

            $new_data[$key]['update_time'] = time();

            if (isset($value->Issn)) {
                $new_data[$key]['issn'] = $value->Issn;
            }
        }

        return $new_data;
    }
    //期刊期数的格式 --输入格式
    public function toBookListFields()
    {
        $data = $this->api_data->Data;
        $new_data = [];

        foreach ($data as $key => $value) {
            if (isset($value->ID)) {
                $new_data[$key]['book_id'] = $value->ID;
            }
            if (isset($value->Name)) {
                $new_data[$key]['book_name'] = $value->Name;
            }
            if (isset($value->Year)) {
                $new_data[$key]['current_update_year'] = $value->Year;
            }
            if (isset($value->Issue)) {
                $new_data[$key]['current_update_month'] = $value->Issue;
            }
            if (isset($value->ShortCode)) {
                $new_data[$key]['short_code'] = $value->ShortCode;
            }
            if (isset($value->CoverImages)) {
                $new_data[$key]['book_img_s'] = $value->CoverImages[0];
                $new_data[$key]['book_img_m'] = $value->CoverImages[1];
                $new_data[$key]['book_img_l'] = $value->CoverImages[2];
                $new_data[$key]['book_img_z'] = $value->CoverImages[3];
            }
        }
        $this->insert_data = $new_data;
        return $this;
    }
    //目录和详情，目录是新增，详情是更新 --输入格式
    public function toBookDetailFields($id)
    {
        $data = $this->api_data->Data;

        $new_data = [];
        $list = [];

        foreach ($data as $key => $value) {
            foreach ($value->Articles as $k => $v) {
                $new_data['book_list_id'] = $id;

                if (isset($value->Column)) {
                    $new_data['column_name'] = $value->Column;
                }
                if (isset($v->ArticleID)) {
                    $new_data['article_id'] = $v->ArticleID;
                }
                if (isset($v->Title)) {
                    $new_data['article_title'] = $v->Title;
                }
                if (isset($v->Author)) {
                    $new_data['author'] = $v->Author;
                }
                if (isset($v->Summary)) {
                    $new_data['summary'] = $v->Summary;
                }
                if (isset($v->FirstImg)) {
                    $new_data['img_url'] = $v->FirstImg;
                }
                if (isset($v->IsCover)) {
                    if ($v->IsCover == 'false') {
                        $new_data['is_cover'] = 0;
                    }else{
                        $new_data['is_cover'] = 1;
                    }
                }
                if (isset($v->FirstImg)) {
                    $new_data['first_img'] = $v->FirstImg;
                }
                if (isset($v->Sequence)) {
                    $new_data['sort'] = $v->Sequence;
                }
                $list[] = $new_data;
            }
        }
        $this->insert_data = $list;
        return $this;
        // $model->article_id = '';
        // $model->article_content = '';
        // $model->content_size = '';
        // $model->key_list = '';
    }
    public function toBookDetailInput()
    {
        $data = $this->api_data->Data;

        // $new_data['article_id'] = $data->ArticleID;

        $new_data['article_title'] = $data->Title;
        $new_data['summary'] = $data->Summary;
        // $new_data['SubTitle'] = '';
        $new_data['column_name'] = $data->Columns;
        // $new_data['Introduction'] = '';
        $new_data['article_content'] = $data->Content;
        $new_data['content_size'] = $data->WordSize;
        // $new_data['Volume'] = '';
        // $new_data['Content'] = $data->article_content;
        // $new_data['WordSize'] = '';
        // $new_data['ImgList'] = [];
        $new_data['key_list'] = json_encode($data->KeyWordList);
        $new_data['book_name'] = $data->MagazineGuid;
        $new_data['book_summary'] = '';
        // $new_data['MagazineName'] = $data[0]->;
        // $new_data['ISSN'] = $data[0]->;
        // $new_data['Year'] = $data[0]->;
        // $new_data['Issue'] = $data[0]->;
        $new_data['sort'] = $data->Sequence;
        // $new_data['created_at'] = time();
        // $new_data['updated_at'] = time();

        $this->insert_data = $new_data;
        return $this;

    }
    //期刊期数输出的格式
    public function toQiKanNumOutPut($data)
    {
        $new_data = [];

        foreach ($data as $key => $value) {
            $new_data[$key]['ID'] = $value->book_id;
            $new_data[$key]['Name'] = $value->book_name;
            $new_data[$key]['Year'] = $value->current_update_year;
            $new_data[$key]['Issue'] = $value->current_update_month;
            $new_data[$key]['ShortCode'] = $value->short_code;
            $new_data[$key]['CoverImages'][0] = $value->book_img_s;
            $new_data[$key]['CoverImages'][1] = $value->book_img_m;
            $new_data[$key]['CoverImages'][2] = $value->book_img_l;
            $new_data[$key]['CoverImages'][3] = $value->book_img_z;
        }
        $this->out_put_data = $new_data;
        return $this;
    }
    //期刊目录输出格式
    public function toQiKanContentListOutPut($data)
    {
        $new_data = [];
        $id = 0;
        foreach ($data as $key => $value) {
            if ($key > 0) {
                //开始分组
                if ($value->column_name != $data[$key-1]->column_name) {
                    $id++;
                }
            }
            $new_data[$id]['Column'] = $value->column_name;
            $new_data[$id]['Articles'][$key]['ArticleID'] = $value->article_id;
            $new_data[$id]['Articles'][$key]['Title'] = $value->article_title;
            $new_data[$id]['Articles'][$key]['Summary'] = $value->summary;
            $new_data[$id]['Articles'][$key]['Sequence'] = $value->sort;
            $new_data[$id]['Articles'][$key]['FirstImg'] = $value->first_img;
            $new_data[$id]['Articles'][$key]['Author'] = $value->author;
        }
        $this->out_put_data = $new_data;
        return $this;
    }
    //期刊文章输出格式
    public function toQikanContentOutPut($data)
    {
        $new_data['ArticleID'] = $data->bookDetail[0]->article_id;
        $new_data['Title'] = $data->bookDetail[0]->article_title;
        $new_data['SubTitle'] = '';
        $new_data['Summary'] = $data->bookDetail[0]->summary;
        $new_data['Introduction'] = '';
        $new_data['Columns'] = $data->bookDetail[0]->column_name;
        $new_data['Volume'] = '';
        $new_data['Content'] = $data->bookDetail[0]->article_content;
        $new_data['WordSize'] = $data->bookDetail[0]->content_size;
        $new_data['ImgList'] = [];
        $new_data['KeyWordList'] = $data->bookDetail[0]->key_list;
        $new_data['MagazineGuid'] = $data->bookDetail[0]->book_list_id;
        $new_data['MagazineName'] = $data->bookDetail[0]->book_name;
        $new_data['ISSN'] = '';
        $new_data['Year'] = $data->issn;
        $new_data['Issue'] = $data->issn;
        $new_data['Sequence'] = $data->bookDetail[0]->sort;

        $this->out_put_data = $new_data;
        return $this;
    }

}

